package com.atguigu.userprofile.MysqlUtil;

import com.atguigu.userprofile.constant.ConstCode;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class MyClickhouseUtil {
    public static void executesql(String sql) {

        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
            String url = MyPropsUtil.get(ConstCode.CK_URL);
            connection = DriverManager.getConnection(url);
            preparedStatement= connection.prepareStatement(sql);
            preparedStatement.execute();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("ckhouse 执行sql失败");
        }finally {
            try {
                if(preparedStatement!=null && !preparedStatement.isClosed()){
                    preparedStatement.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(connection!=null && !connection.isClosed()){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
